package com.tridoron.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tridoron.reggie.common.CustomException;
import com.tridoron.reggie.dto.DishDto;
import com.tridoron.reggie.entity.Dish;
import com.tridoron.reggie.entity.DishFlavor;
import com.tridoron.reggie.entity.Setmeal;
import com.tridoron.reggie.entity.SetmealDish;
import com.tridoron.reggie.mapper.DishMapper;
import com.tridoron.reggie.service.DishFlavorService;
import com.tridoron.reggie.service.DishService;
import com.tridoron.reggie.service.SetmealDishService;
import com.tridoron.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * 新增菜品，同时保存对应的口味数据
     * @param dishDto
     */
    @Transactional
    @Override
    public void saveWithFlavor(DishDto dishDto) {
        //保存菜品的基本信息到菜品表
        this.save(dishDto);

        Long dishId=dishDto.getId();
        //给菜品口味绑定菜品id
        List<DishFlavor> flavors=dishDto.getFlavors();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dishId);
        }
        //保存菜品口味数据到菜品口味表
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 根据id查询菜品信息和对应的口味信息
     * @param id
     * @return
     */
    @Override
    public DishDto getByIdWithFlavor(Long id) {
        Dish dish = this.getById(id);
        DishDto dishDto=new DishDto();
        BeanUtils.copyProperties(dish,dishDto);

        //查询当前菜品对应的口味信息
        LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper=new LambdaQueryWrapper<>();
        dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId,dish.getId());
        List<DishFlavor> flavors = dishFlavorService.list(dishFlavorLambdaQueryWrapper);
        dishDto.setFlavors(flavors);
        return dishDto;
    }

    /**
     * 更新表数据
     * @param dishDto
     */
    @Transactional
    @Override
    public void updateWithFlavor(DishDto dishDto) {
        //更新dish表
        this.updateById(dishDto);

        //删除原有口味
        LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper=new LambdaQueryWrapper<>();
        dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(dishFlavorLambdaQueryWrapper);

        //添加新口味
        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dishDto.getId());
        }
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 修改status属性
     * @param status
     * @param ids
     */
    @Transactional
    @Override
    public void updateStatusById(Integer status, Long[] ids) {
        for (Long id : ids) {
            Dish dish=this.getById(id);
            if (dish.getStatus()!=status){
                dish.setStatus(status);
            }
            dishService.updateById(dish);
        }
        //判断套餐中是否有管理的菜品，如果有，则进行停售
        if (status==0){
            List<Long> dishIds = Arrays.asList(ids);
            LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper=new LambdaQueryWrapper<>();
            setmealDishLambdaQueryWrapper.in(SetmealDish::getDishId,dishIds);
            List<SetmealDish> list = setmealDishService.list(setmealDishLambdaQueryWrapper);
            List<Long> setmealIdList = list.stream().map(SetmealDish::getSetmealId).collect(Collectors.toList());
            for (Long setmealId : setmealIdList) {
                Setmeal setmeal = setmealService.getById(setmealId);
                if (setmeal.getStatus()!=0){
                    setmeal.setStatus(0);
                }
                setmealService.updateById(setmeal);
            }
        }

    }

    /**
     * 删除菜品信息
     * @param ids
     */
    @Transactional
    @Override
    public void remove(Long[] ids) {
        //查询菜品状态，确定是否可用删除
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper();
        dishLambdaQueryWrapper.in(Dish::getId,ids);
        dishLambdaQueryWrapper.eq(Dish::getStatus,1);

        int count = this.count(dishLambdaQueryWrapper);
        if(count > 0){
            //如果不能删除，抛出一个业务异常
            throw new CustomException("菜品正在售卖中，不能删除");
        }

        this.removeByIds(Arrays.asList(ids));

        LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishFlavorLambdaQueryWrapper.in(DishFlavor::getDishId,ids);
        dishFlavorService.remove(dishFlavorLambdaQueryWrapper);
    }
}
